package com.study.talk.ui.app.change

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.study.common.DateUtil
import com.study.common.ToastUtil
import com.study.common.VerificationUtil
import com.study.talk.data.model.UserInfo
import com.study.talk.ui.app.base.BaseViewModel
import com.study.talk.utils.extras.useless
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import java.util.Calendar
import javax.inject.Inject

@HiltViewModel
class ChangeUserInfoViewModel @Inject constructor(application: Application) :
    BaseViewModel(application) {

    val userId = MutableLiveData(-1)
    val username = MutableLiveData("")
    val phone = MutableLiveData("")
    val nickName = MutableLiveData("")
    val email = MutableLiveData("")
    val address = MutableLiveData("")
    val birth = MutableLiveData("请选择生日")
    val introduce = MutableLiveData("")
    val avatar = MutableLiveData("")
    val gender = MutableLiveData(1)

    val complete = MutableLiveData<UserInfo?>(null)


    fun loadUserInfo() {
        app.userInfo.let {
            userId.postValue(it.id)
            username.postValue(it.username)
            phone.postValue(it.phone)
            nickName.postValue(it.nickName)
            email.postValue(it.email)
            address.postValue(it.address)
            birth.postValue(it.birth)
            introduce.postValue(it.introduce)
            gender.postValue(it.gender)
            avatar.postValue(it.avatar)
        }
    }

    fun changeBirth(year: Int, month: Int, day: Int) {
        val calendar = Calendar.getInstance()
        calendar[Calendar.YEAR] = year
        calendar[Calendar.MONTH] = month
        calendar[Calendar.DAY_OF_MONTH] = day
        birth.postValue(DateUtil.DateFormat.format(calendar.time))
    }

    fun check(): Boolean {
        if (nickName.value?.useless(app, "请输入昵称") == true) return false
        if (email.value?.useless(app, "请输入邮箱") == true) return false
        if (address.value?.useless(app, "请输入住址") == true) return false
        if (introduce.value?.useless(app, "请输入简介") == true) return false
        if (avatar.value?.useless(app, "请上传头像") == true) return false
        if (birth.value?.useless(app, "请选择生日") == true) return false

        if (birth.value == "请选择生日") {
            ToastUtil.showToast(app, "请选择生日")
            return false
        }

        if (!VerificationUtil.isValidEmailAddress(email.value!!)) {
            ToastUtil.showToast(app, "请输入正确的邮箱")
            return false
        }

        return true
    }

    fun submit() {
        viewModelScope.launch {
            val user = UserInfo()
            user.id = userId.value
            user.username = username.value
            user.phone = phone.value
            user.nickName = nickName.value?.trim()
            user.email = email.value?.trim()
            user.address = address.value?.trim()
            user.introduce = introduce.value?.trim()
            user.birth = birth.value
            user.avatar = avatar.value
            user.gender = gender.value
            user.bgImg = ""
            val result = userRepository.updateUserInfo(user)
            if (result == null) {
                ToastUtil.showToast(app, "更新用户信息失败,result is null")
                return@launch
            }
            complete.postValue(result)
        }
    }

    fun upload(path: String) {
        viewModelScope.launch {
            val result = fileRepository.upload(path)
            if (result.isNotEmpty()) {
                avatar.postValue(result)
            } else {
                ToastUtil.showToast(app, "头像上传失败")
            }
        }
    }
}